package com.beerandjesus.blackjack.deck;

import com.beerandjesus.blackjack.deck.Card;

import java.util.*;

/**
 * User: reverend
 * Date: Jul 1, 2005
 * Time: 12:28:28 AM
 */
public class DeckOfCards {
    private Iterator iterator;
    private List deck;
    private int count;

    public DeckOfCards() {
        createAllCards();
        shuffle();
        count = 0;
    }

    private void createAllCards() {
        this.deck = new ArrayList();
        for (int i = 1; i <= 13; i++) {
            deck.add(new Card(i, Suit.HEART));
            deck.add(new Card(i, Suit.SPADE));
            deck.add(new Card(i, Suit.DIAMOND));
            deck.add(new Card(i, Suit.CLUB));
        }

    }

    public void shuffle() {
        Collections.shuffle(deck);        
    }

    public Card nextCard() {
        Card card = (Card) iterator().next();
        count++;
        return card;
    }

    public boolean hasNext() {
        return iterator().hasNext();
    }

    private Iterator iterator() {
        if (iterator == null) {
            iterator = deck.iterator();
        }
        return iterator;
    }

    public int remainingSize() {
        return 52 - count;
    }

    public void refresh() {
        iterator = null;
        count = 0;
        createAllCards();
        shuffle();
    }

    public void stack(Card card) {
        deck.add(0, card);
    }
}
